from rest_framework import serializers


class PeopleinfoSerializers(serializers.Serializer):

    name = serializers.CharField()

class BookinfoSerializers(serializers.Serializer):

    id = serializers.IntegerField(required=False) #required = True 默认字典转数据时必须传的字段
    name = serializers.CharField()
    pub_date = serializers.DateField()
    readcount = serializers.IntegerField()
    commentcount = serializers.IntegerField()
    is_delete = serializers.BooleanField()


    #级联关系
    # 序列化器中 id 可以获取 模型中 隐藏的id数据
    people = PeopleinfoSerializers(many = True)
from book.models import BookInfo
# class PeopleinfoSerializers(serializers.Serializer):
#
#     id = serializers.IntegerField()
#     name = serializers.CharField()
#     gender = serializers.IntegerField()
#     description = serializers.CharField()
#
#
#     #book_id对应的IntegerField   book不可以
#     # book_id = serializers.IntegerField()
#
#
#     ''' 返回 id
#     query = 获取外键的所有数据
#     read_only 只是读取数据获取外键数据
#     '''
#     # book = serializers.PrimaryKeyRelatedField(queryset=BookInfo.objects.all())
#     # book = serializers.PrimaryKeyRelatedField(read_only=True)
#
#     '''返回书籍名字
#     字段将被序列化为关联对象的字符串（即____str__方法的返回值） 字符串数据
#     '''
#     # book = serializers.StringRelatedField()
#
#     '''序列化数据嵌套序列化数据'''  #{'id': 6, 'name': '乔峰', 'gender': 1, 'description': '降龙十八掌',
#     # 'book': OrderedDict([('id', 2), ('name', '天龙八部'), ('pub_date', '1986-07-24'), ('readcountommentcount', 40), ('is_delete', False)])}
#
#     book = BookinfoSerializers()